package com.didibigdata.Repositories;

import com.didibigdata.entities.OrderNum;
import com.didibigdata.entities.OrderNumSection;
import com.didibigdata.utils.JDBCTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lkabb on 2020/6/7
 */
public class OrderNumSectionRepository {
    public List<OrderNumSection> getOrderNumSection() {
        //日志
        Logger logger = LoggerFactory.getLogger(OrderNumSectionRepository.class);
        logger.info("add OrderNumSectionRepository logger successful!");

        ResultSet resultSetmysql = null;
        Connection connectionmysql = null;
        PreparedStatement statementmysql = null;
        List<OrderNum> orderNumList = new ArrayList<OrderNum>();

        // 定义SQL语句
        List<OrderNumSection> orderListInWorkday = new ArrayList<OrderNumSection>();
        List<OrderNumSection> orderListInWeekend = new ArrayList<OrderNumSection>();
        List<OrderNumSection> orderListInHoliday = new ArrayList<OrderNumSection>();
        List<OrderNumSection> orderList = new ArrayList<OrderNumSection>();
        //定义每个时段订单数 第一维：工作日 周末 节假日 第二维：count0to5 count6to10 count11to15 , count16to20 , count21to23 ;
        int[][] countInTimeSection = new int[3][24];
        for (int i = 0; i < 3 ; i++ ) {
            for (int j = 0; j < 24 ; j++) {
                countInTimeSection[i][j] = 0;
            }
        }
        try{
            connectionmysql = JDBCTools.getConnection();
            String sqltime = "select time,count,day from t_didi2016_timests";
            statementmysql = connectionmysql.prepareStatement(sqltime);
            logger.info("connect to JDBC and executeQuery [{}]", sqltime);
            resultSetmysql = statementmysql.executeQuery();

            //工作日 ：8 9 10 11 12 13 14 17 18 19 20 21 24 25 26 27 28 31
            // 周末： 15 16 22 23 29 30
            // 假期： 1 2 3 4 5 6 7

            int acount1 = 0;
            int acount2 = 0;
            int acount3 = 0;
            while (resultSetmysql.next()) {
                OrderNum orderNum = new OrderNum();
                orderNum.setDay(resultSetmysql.getString(1));
                orderNum.setCount(Integer.parseInt(resultSetmysql.getString(2)));
                orderNumList.add(orderNum);

                //时间段 0-5  6-10  11-15  16-20   21-23
                int time = Integer.parseInt(resultSetmysql.getString(1));
                int count = Integer.parseInt(resultSetmysql.getString(2));
                int day1 = Integer.parseInt(resultSetmysql.getString(3));
                int day2;
                if (day1 < 20161100) {
                    day2 = day1 - 20161000;
                } else {
                    day2 = day1 - 20161100 + 31;
                }
                int day3 = day2 % 7;

                if((day2 > 7) && ((day3==3) || (day3==4) || (day3==5) || (day3==6) || (day3==0))) {  //工作日
                    acount1++;
                    for (int a = 0; a < 24; a++) {
                        if (time == a) {
                            countInTimeSection[0][a] += count;
                        }
                    }
                }
                else if((day2 > 7) && ((day3==1) || (day3==2))) { //周末
                    acount2++;
                    for (int a = 0; a < 24; a++) {
                        if (time == a) {
                            countInTimeSection[1][a] += count;
                        }
                    }
                }
                else if (day2 >= 1 && day2 <= 7){ //假期
                    acount3++;
                    for (int a = 0; a < 24; a++) {
                        if (time == a) {
                            countInTimeSection[2][a] += count;
                        }
                    }
                }
            }
            logger.info("set countInTimeSection list successful!");

            int resAcount1 = acount1 / 24;
            int resAcount2 = acount2 / 24;
            int resAcount3 = acount3 / 24;

            logger.info("count of workday : [{}]", resAcount1);
            logger.info("count of weekend : [{}]", resAcount2);
            logger.info("count of holiday : [{}]", resAcount3);

            for (int i = 0; i < 24; i++) {
                countInTimeSection[0][i] = countInTimeSection[0][i] / resAcount1;
            }
            for (int i = 0; i < 24; i++) {
                countInTimeSection[1][i] = countInTimeSection[1][i] / resAcount2;
            }
            for (int i = 0; i < 24; i++) {
                countInTimeSection[2][i] = countInTimeSection[2][i] / resAcount3;
            }


            for (int i = 0; i < 24; i++) {
                orderList.add(new OrderNumSection(i + "时",countInTimeSection[0][i]));
            }
            for (int i = 0; i < 24; i++) {
                orderList.add(new OrderNumSection(i + "时",countInTimeSection[1][i]));
            }
            for (int i = 0; i < 24; i++) {
                orderList.add(new OrderNumSection(i + "时",countInTimeSection[2][i]));
            }

        }catch (Exception e) {
            logger.error("OrderNumSectionRepository SQLException: [{}]", e);
            e.printStackTrace();
        } finally {
            JDBCTools.releaseConnection(connectionmysql, statementmysql, resultSetmysql);
        }

        return orderList;
    }
}
